#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "InetAddr.hpp"
#include "Log.hpp"
using namespace std;
using namespace my_log;
const string PATH = "./dict.txt";
const string MARK = ": ";
class Dict
{
private:
    void Open()
    {
        ifstream in(_path);
        if (!in.is_open())
        {
            LOG(Level::FATAL) << "dict open fail!";
            exit(1);
        }
        string str;
        while(getline(in,str))
        {
            int p = str.find(MARK);
            if(p==0)
            {
                LOG(Level::ERROR)<<"find error";
                continue;
            }
            string En(str.substr(0,p));
            string Ch(str.substr(p+MARK.size()));
            _data.emplace_back(En,Ch);
            LOG(Level::INFO)<<"载入"<<str;
        }
    }
public:
    Dict(string path = PATH)
        : _path(path)
    {
        Open();
    }
    string Translate(string& str,InetAddr& addr)
    {
        for(auto [en,ch]:_data)
        {
            if(str == en) return ch;
        }
        return "None";
    }

private:
    string _path;
    vector<pair<string, string>> _data;
};